package com.efounder.anim;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.efounder.mobilemall.R;

/**
 * Created by wei on 2018/1/18/018.
 */

public class MyPopWindow extends PopupWindow {

    private Context mContext;

    private View view;

    private ImageView close;
//    private  TextView confirm;


    public MyPopWindow(Context mContext, View.OnClickListener itemsOnClick,int layout,boolean isbuyNow) {

        this.view = LayoutInflater.from(mContext).inflate(layout, null);

        close = (ImageView) view.findViewById(R.id.close);
//        confirm = (TextView) view.findViewById(R.id.confirm);

        if(isbuyNow){
            close.setImageDrawable(mContext.getResources().getDrawable(R.drawable.arrow));
        }
        // 取消按钮
//        confirm.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                dismiss();
//            }
//        });
        close.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // 销毁弹出框
                dismiss();
            }
        });
        // 设置按钮监听
//        confirm.setOnClickListener(itemsOnClick);

        // 设置外部可点击
        this.setOutsideTouchable(true);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        this.view.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = view.findViewById(R.id.pop_layout).getTop();

                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });


    /* 设置弹出窗口特征 */
        // 设置视图
        this.setContentView(this.view);
        // 设置弹出窗体的宽和高
        this.setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);

        // 设置弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(false);//获取外部触摸事件

        // 设置弹出窗体显示时的动画，从底部向上弹出
        this.setAnimationStyle(R.style.pop_anim);

    }

}